/*
 * @Description:
 * @Author: Zzt
 * @Date: 2023-02-20 11:23:19
 * @LastEditTime: 2023-02-28 15:07:33
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  GET_CHANNEL_LIST,
  CELECT_CHANNEL,
  DEL_CHANNELLIST,
  SET_CHANNEL,
  GET_ANNOUNCE_LIST,
  SET_SELECT_KEY,
  ADD_ANNOUNCELIST,
  DEL_ANNOUNCELIST,
  EDIT_ANNOUNCE_LIST
} from '../constants';
import { AnnouncementMgtReducer } from '../reducers/AnnouncementMgtReducer';
import { message, Modal } from 'antd';

import { fetchChannelList, saveChannel, delectChannelById } from '@/api/announcement/channel';
import {
  fetchAnnouncementByPage,
  saveAnnouncement,
  editAnnouncement,
  delectAnnouncement
} from '@/api/announcement/announcement';

const initialState = {
  selectItems: [],
  channelList: [],
  noticeTypeId: 0,
  announceList: [],
  statusList: [
    { id: 1, label: '待发布' },
    { id: 2, label: '已发布' },
    { id: 3, label: '已撤回' }
  ]
};
const AnnouncementMgtContext = createContext();
const AnnouncementMgtContextProvider = (props) => {
  const [state, dispatch] = useReducer(AnnouncementMgtReducer, initialState);
  const actions = {
    /* 获取频道列表 */
    getChannelList() {
      fetchChannelList({}).then((res) => {
        const { data } = res;
        dispatch({ type: GET_CHANNEL_LIST, data });
      });
    },
    /* 列表选中的值 */
    setSelectChannelId(data) {
      dispatch({ type: CELECT_CHANNEL, data });
    },
    /* 删除频道 */
    delChannel(id, title = '确认删除该频道？') {
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          delectChannelById(ids).then((res) => {
            message.success('删除成功');
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DEL_CHANNELLIST, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    /* 新增频道 */
    setChannnel(params) {
      return new Promise((resolve, reject) => {
        saveChannel(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_CHANNEL, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 设置选中列表 */
    setSelectKey(data) {
      dispatch({ type: SET_SELECT_KEY, data });
    },

    addAnnounceList(params) {
      return new Promise((resolve, reject) => {
        saveAnnouncement(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: ADD_ANNOUNCELIST, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 获取通知列表 */
    getAnnounceList(params) {
      return new Promise((resolve, reject) => {
        fetchAnnouncementByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: GET_ANNOUNCE_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => {
            dispatch({ type: GET_ANNOUNCE_LIST, data: [] });
            reject(err);
          });
      });
      // TODO 调取获取列表接口
    },
    /* 删除选择项 */
    delAnnounce(id, title = '确认删除该公告？') {
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          delectAnnouncement(ids).then((res) => {
            message.success('删除成功');
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DEL_ANNOUNCELIST, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    /* 修改公告 */
    editStatus(params) {
      dispatch({ type: EDIT_ANNOUNCE_LIST, data: params });
    },
    editAnnounce(params) {
      return new Promise((resolve, reject) => {
        editAnnouncement({ ...params })
          .then((res) => {
            const { data } = res;
            dispatch({ type: EDIT_ANNOUNCE_LIST, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    }
  };
  useEffect(() => {}, []);
  const filters = {
    statusFilter(id) {
      return state.statusList.find((item) => item.id === id)?.label || '-';
    }
  };
  return (
    <AnnouncementMgtContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </AnnouncementMgtContext.Provider>
  );
};
export const useAnnouncementMgtContext = () => {
  return useContext(AnnouncementMgtContext);
};
export { AnnouncementMgtContext, AnnouncementMgtContextProvider };
